#include "NumberComparable.h"

NumberComparable::NumberComparable()
{
	position_ = -1;
	number_ = "";
}

NumberComparable::NumberComparable(int pos, std::string num)
{
	position_ = pos;
	number_ = num;
}

std::string NumberComparable::getNumber() const
{
	return number_;
}

bool NumberComparable::compare(NumberComparable b)  //only non-intuitive comparison function
{
	std::string s = b.getNumber();
	int ai = 0;
	int bi = 0;
	int aj = number_.size()-1;
	int bj = s.size()-1;

	//strip insignificant zeroes (decimals) off end of each number

	while (aj >= 0)
	{
		if (number_[aj] == '0')
			aj--;

		else if (number_[aj] == '.')
		{
			aj--;
			break;
		}

		else
			break;
	}
		
	while (bj >= 0)
	{
		if (s[bj] == '0')
			bj--;

		else if (s[bj] == '.')
		{
			bj--;
			break;
		}

		else
			break;
	}

	if ((aj < 0) && (bj < 0))
		return true;
	if ((aj != bj) && ((aj < 0) || (bj < 0)))
		return false;

	//strip insignificant zeroes (decimals) off beginning of each number
	
	while (ai < aj)
	{
		if (number_[ai] == '0')
			ai++;
		else
			break;
	}
	

	while (bi < bj)
	{
		if (s[bj] == '0')
			bi++;
		else
			break;
	}


	if ((aj - ai) != (bj - bi))  //numbers without insignificant zeroes of different length are not equal
		return false;


	for (int i = 0; ai+i <= aj, bi+i <= bj; i++)  //compare the remaining numbers
	{
		if (number_[ai+i] != s[bi+i])
			return false;
	}
		
	return true;
}
